<?php

namespace Admin\Controller;

use Think\Upload;
class BrandController extends BaseController{
    
    public function index(){
        
        // 获取品牌数据
        $brand_M = M('brand');
        
        $brands = $brand_M->select();
        
        // 传递数据
        $this->assign('brands',$brands);
        
        $this->display('brandlist');
    }
    
    
    public function addBrand(){
        
        if (IS_POST){
            
            // 获取表单数据
            $data = I('post.');
            
            // 验证表单数据
            $brand_M = D('Brand');
            
            if (!$brand_M->create($data)){
                $this->ajaxReturn(array('status'=>0,'msg'=>$brand_M->getError()));
            }
            
            // 获取上传图片
            if (!file_exists(__ROOT__.'uploads')){
                // 创建文件上传保存路径
                $res = mkdir(__ROOT__.'uploads');
                
            }
            if($_FILES['logo']['error'] == 0 && !empty($_FILES['logo'])){
                // 创建文件上传的对象
                $upload = new Upload();
                
                // 设置文件上传的最大尺寸
                $upload->maxSize = 2*1024*1024;
                
                // 设置上传文件保存路径
                $upload->savePath = __ROOT__.'uploads';
                
                // 限制文件上传的类型
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
                
                $info = $upload->upload();
                
                if(!$info){
                    $this->ajaxReturn(array('status'=>0,'msg'=>$upload->getError()));
                }
                
                // 添加数据
                $data['logo'] = '/uploads/'.$info['logo']['savepath'].$info['logo']['savename'];
            }else {
                unset($data['logo']);
            }
            
            if($data['brand_id']>0){
                
                $res = $brand_M->where(array('id'=>I('post.brand_id')))->save($data);
            }else{
                if ($brand_M->where(array('brand_name'=>I('post.brand_name')))->find()){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'品牌名称已经存在'));
                }
                $res = $brand_M->add($data);
            }
            
            if ($res !== false){
                if ($res == 0){
                    $this->ajaxReturn(array('status'=>2,'msg'=>'数据未修改'));
                }
                $this->ajaxReturn(array('status'=>1,'msg'=>'添加成功'));
            }else{
                $this->ajaxReturn(array('status'=>0,'msg'=>'添加失败，请重试'));
            }
        }elseif(I('get.brand_id') > 0){
            $brand_id = I('get.brand_id');
            // 获取品牌
            $brand = M('brand')->find($brand_id);
            
            $this->assign('brand',$brand);
            
            $this->display('brandAdd');
        }else {
            
            $this->display('brandAdd');
        }
        
    }
    
    public function brand_del(){
        // 获取品牌id
        $brand_id = I('get.brand_id');
        
        // 获取品牌logo图片
        $logo = M('brand')->where(array('id'=>$brand_id))->getField('logo');
        
        // 删除品牌
        $res = M('brand')->delete($brand_id);
        
        if($res !== false){
            // 删除logo
            @unlink(trim($logo,'/'));
            $this->ajaxReturn(array('status'=>1));
        }else{
            $this->ajaxReturn(array('status'=>0));
        }
    }
}
